package design.pattern.Lu23BridgePattern;

import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @program JavaBase
 * @description: 桥接模式
 * @author: zhanglu
 * @create: 2020-05-20 13:52:00
 */
public class BridgePattern02 {

    public static void main(String[] args) {
//        Implementor implementor = new ConcreteImplementor01();
//        Abstraction abstraction = new RefinedAbstraction(implementor);
//        abstraction.request();


        Person p1 = new Person("1","111",1);
        Person p2 = new Person ("1","222",0);
        Person p3 = new Person ("3","333",1);
        Person p4 = new Person ("4","444",0);
        Person p5 = new Person ("4","555",1);
        List<Person > sourceList = new ArrayList<>(); sourceList.add(p1);
        sourceList.add(p2);sourceList.add(p3); sourceList.add(p4); sourceList.add(p5);
        List<Person> unique = coverDuplicate(sourceList);
        unique.forEach(e -> System.out.println(e.getPersonId()+","+e.getName()+","+e.getOperateTag()));
    }

    //去重复，后面覆盖前面的
    public static List<Person> coverDuplicate(List<Person> sourceList) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return new ArrayList<>();
        }
        List<Person> distinctList = sourceList.stream().collect(
                Collectors.toMap(Person::getPersonId, Function.identity(), (e1, e2) -> e2)
        ).values().stream().collect(Collectors.toList());
        return distinctList;
    }

}

@Data
class Person{
    private String personId;
    private String name;
    private Integer operateTag;

    public Person(String personId, String name, Integer operateTag) {
        this.personId = personId;
        this.name = name;
        this.operateTag = operateTag;
    }
}

interface Implementor{
    void todo01();
    void todo02();
}

class ConcreteImplementor01 implements Implementor{
    @Override
    public void todo01() {
        System.out.println("todo01...");
    }
    @Override
    public void todo02() {
        System.out.println("todo02...");
    }
}
class ConcreteImplementor02 implements Implementor{
    @Override
    public void todo01() {
        System.out.println("todo01...");
    }
    @Override
    public void todo02() {
        System.out.println("todo02...");
    }
}

abstract class Abstraction{
    private Implementor imp;
    public Abstraction(Implementor imp){
        this.imp = imp;
    }
    public Implementor getImp(){
        return this.imp;
    }
    public void request(){
        this.imp.todo01();
    }
}

class RefinedAbstraction extends Abstraction{
    public RefinedAbstraction(Implementor imp) {
        super(imp);
    }
    @Override
    public void request() {
        super.request();
        System.out.println("未完，待...");
        super.getImp().todo02();
    }
}

